package com.mine.modules.vm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mine.common.domain.vo.Result;
import com.mine.common.utils.ToTreeUtil;
import com.mine.modules.vm.domain.query.LayerQuery;
import com.mine.modules.vm.entity.VmLayer;
import com.mine.modules.vm.entity.VmPermissionLayer;
import com.mine.modules.vm.entity.VmSensorTree;
import com.mine.modules.vm.mapper.VmSensorTreeMapper;
import com.mine.modules.vm.service.IVmSensorTreeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 传感器分类 服务实现类
 * </p>
 *
 * @author xujf
 * @since 2024-11-08
 */
@Service
public class VmSensorTreeServiceImpl extends ServiceImpl<VmSensorTreeMapper, VmSensorTree> implements IVmSensorTreeService {

    @Override
    public Result<?> listAll(String keywords) {
        try {
            LambdaQueryWrapper<VmSensorTree> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.and(StringUtils.isNotBlank(keywords), e -> e
                    .like(VmSensorTree::getTreeName, keywords)
                    .or()
                    .like(VmSensorTree::getTreeCode, keywords)
            );
            queryWrapper.orderByAsc(VmSensorTree::getParentId);
            List<VmSensorTree> list = this.list(queryWrapper);
            List<VmSensorTree> treeList = new ArrayList<>();
            //如果有菜单名查询条件，则平铺数据 不做上下级
            if (StringUtils.isNotEmpty(keywords)) {
                if (list != null && !list.isEmpty()) {
                    treeList = list;
                }
            } else {
                ToTreeUtil.getTreeList(treeList, list, null);
            }
            return Result.OK("查询成功！", treeList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }

}
